#include "trans_util.h"
#include "data_type.h"
#include <stdint.h>
#include <termios.h>
#include <stdio.h>
#include <libubus.h>
#include <libubox/uloop.h>
#include <libubox/list.h>
#include <libubox/blobmsg_json.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

struct blob_buf mqtt_buf;
void send_data_to_energy(){

}
void send_data_to_mqtt(char *json){
    // 连接 ubus
    struct ubus_context *ctx = ubus_connect(NULL);
    if (!ctx) {
        fprintf(stderr, "Failed to connect to ubus\n");
        return -1;
    }
    // 查找 network.device 的 ID
    uint32_t obj_id;
    if (ubus_lookup_id(ctx, "sihan.mqtt", &obj_id) != 0) {
        fprintf(stderr, "Failed to lookup sihan.mqtt\n");
        ubus_free(ctx);
        return -1;
    }

    blob_buf_init(&mqtt_buf, 0);
    
    blobmsg_add_string(&mqtt_buf, "json", json);
    // 发起异步调用
    int ret = ubus_invoke(ctx, obj_id, "send", mqtt_buf.head, NULL, 0, 3000);
    if (ret == 0) {
        
    }
    // 清理资源
    blob_buf_free(&mqtt_buf);
    ubus_free(ctx);
}
